/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 * \file     Mcu_KickCore.c                                                                             *
 * \brief    AUTOSAR 4.3.1 MCAL Mcu Driver                                                              *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2023/08/02     <td>1.0.0                                                                     *
 * </table>                                                                                             *
 *******************************************************************************************************/
/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Mcu_ResetSig.h"
#include "Mcu_Reset.h"
#include "Mcu_KickCore.h"
#include "Mcu_Fault.h"
#include "Mcal_ScrBits.h"
#include "Mcal_Scr.h"
#include "Mcal.h"
#include "regs_base.h"

/********************************************************************************************************
 *                                  Global Constant Declarations                                        *
 *******************************************************************************************************/
#define MCU_START_SEC_CONST_UNSPECIFIED
#include "Mcu_MemMap.h"
/* PRQA S 3218 1 */
static const Mcal_ScrSignalType vecTableSignal[KICK_CPU_INDEX_4][2U] =
{
    /* CR52 CORE0*/
    {
        SCR_SF_R52P_CFGVECTABLE0, SCR_SF_R52P_C0_VECTABLE_CFG_EN
    },
    /* CR52 CORE1*/
    {
        SCR_SF_R52P_CFGVECTABLE1, SCR_SF_R52P_C1_VECTABLE_CFG_EN,
    },
    /* CR52 CORE2*/
    {
        SCR_SF_R52P_CFGVECTABLE2, SCR_SF_R52P_C2_VECTABLE_CFG_EN,
    },
    {
        SCR_SF_R52P_CFGVECTABLE3, SCR_SF_R52P_C3_VECTABLE_CFG_EN,
    }
};

/* PRQA S 3218 1 */
static const Mcal_ScrSignalType remapCr5LpAddress = SCR_LP_FMMU_LP_AW_ADDR_OFFSET;
/* PRQA S 3218 1 */
static const Mcal_ScrSignalType remapCr5LpEn = SCR_LP_FMMU_LP_AW_REMAP_EN;

#define MCU_STOP_SEC_CONST_UNSPECIFIED
#include "Mcu_MemMap.h"

/********************************************************************************************************
 *                                  Private Function Declarations                                       *
 *******************************************************************************************************/
#define MCU_START_SEC_CODE
#include "Mcu_MemMap.h"

/** *****************************************************************************************************
 * \brief set vector table for cr52 core.
 *
 * \verbatim
 * Syntax             : static Std_ReturnType Mcu_Ip_SetVect(const Mcal_ScrSignalType * vecAddressPtr, const Mcal_ScrSignalType * vecEnPtr, uint32 addr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : vecAddressPtr - scr vector table address signal pointer
 *                      vecEnPtr - scr vector table en signal pointer
 *                      addr - vector table address
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errId
 *
 * Description        : set vector table for cr52 core.
 * \endverbatim
 *******************************************************************************************************/
static Std_ReturnType Mcu_Ip_SetVect(const Mcal_ScrSignalType *vecAddressPtr,
                                     const Mcal_ScrSignalType *vecEnPtr, uint32 addr)
{
    Std_ReturnType status = E_OK;
    uint32 address = addr;
    boolean ret;
    uint32 value;

    /* should be 32bit aligned */
    if (0U == (address & 0x1FU))
    {
        address = address >> 5U;
        ret = Mcal_ScrSetBit(vecAddressPtr, address);

        McuFaultInj_Mcu_Ip_SetVect_01();

        if (E_OK == ret)
        {
            ret = Mcal_ScrSetBit(vecEnPtr, 1U);
            McuFaultInj_Mcu_Ip_SetVect_02();
            if (E_OK == ret)
            {
                /* CR52 ROM Sticky Bit, bit 0/1 need set to 1, bit 0 for debug, bit 1 for remap */
                value = readl(APB_ROMC_BASE + 0x4U);
                value |= 3U;
                writel(value, (APB_ROMC_BASE + 0x4U));
            }
            else
            {
                status = MCU_E_PARAM_CONFIG;
            }
        }
        else
        {
            status = MCU_E_PARAM_CONFIG;
        }
    }
    else
    {
        status = MCU_E_UNALIGNED;
    }

    return status;
}

/** *****************************************************************************************************
 * \brief set remap address for lp core.
 *
 * \verbatim
 * Syntax             : static Std_ReturnType Mcu_Ip_RemapVect(Mcu_KickCoreType core, const Mcal_ScrSignalType *vecAddressPtr,
                                       const Mcal_ScrSignalType *vecEnPtr, uint32 addr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : core - core id
 *                      vecAddressPtr - scr remap address signal pointer
 *                      vecEnPtr - scr remap en signal pointer
 *                      addr - remap address
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errId
 *
 * Description        : set remap address for lp core.
 * \endverbatim
 *******************************************************************************************************/
static Std_ReturnType Mcu_Ip_RemapVect(Mcu_KickCoreType core, const Mcal_ScrSignalType *vecAddressPtr,
                                       const Mcal_ScrSignalType *vecEnPtr, uint32 addr)
{
    Std_ReturnType status = E_OK;
    uint32 address = addr;
    boolean ret;
    uint32 value;

    /* should be 4KB aligned */
    if (0U == (address & 0xFFFU))
    {
        address = address >> 12U;

        if (KICK_CPU_INDEX_4 == core)
        {
            /* se core remap scr is at seip */
            value = readl(APB_SEIP_BASE + 0x30U);
            value &= ~(0xFFFFFU);
            value |= address;
            value |= (1UL << 20U);
            writel(value, (APB_SEIP_BASE + 0x30U));

            /* SE ROM Sticky Bit, bit1 and bit 0 set to 1 */
            value = readl(APB_ROMC_BASE + 0x5CU);
            /* kick se core must set the bit0 and bit1 as 1, 12/20 */
            value |= 3U;
            writel(value, (APB_ROMC_BASE + 0x5CU));
        }
        else /* if (KICK_CPU_INDEX_5 == core) */
        {
            ret = Mcal_ScrSetBit(vecAddressPtr, address);
            if (E_OK == ret)
            {
                ret = Mcal_ScrSetBit(vecEnPtr, 1U);
                if (E_OK != ret)
                {
                    status = MCU_E_PARAM_CONFIG;
                }
            }
            else
            {
                status = MCU_E_PARAM_CONFIG;
            }
        }
    }
    else
    {
        status = MCU_E_UNALIGNED;
    }

    return status;
}

/** *****************************************************************************************************
 * \brief This function set start address.
 *
 * \verbatim
 * Syntax             : static Std_ReturnType Mcu_Ip_SetCoreVect(Mcu_KickCoreType core, uint32 addr)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : core - core id
 *                      addr - start address
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errID
 *
 * Description        : This function set start address.
 * \endverbatim
 *******************************************************************************************************/
static Std_ReturnType Mcu_Ip_SetCoreVect(Mcu_KickCoreType core, uint32 addr)
{
    Std_ReturnType errStatus;

    if (core <= KICK_CPU_INDEX_3)
    {
        errStatus = Mcu_Ip_SetVect(&vecTableSignal[core][0U], &vecTableSignal[core][1U], addr);
    }
    else if (core == KICK_CPU_INDEX_4)
    {
        errStatus = Mcu_Ip_RemapVect(core, NULL_PTR, NULL_PTR, addr);
    }
    else if (core == KICK_CPU_INDEX_5)
    {
        errStatus = Mcu_Ip_RemapVect(core, &remapCr5LpAddress, &remapCr5LpEn, addr);
    }
    else
    {
        errStatus = MCU_E_PARAM_CONFIG;
    }

    return errStatus;
}

/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
/** *****************************************************************************************************
 * \brief This function kickcore.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_SocKickCore(Mcu_KickCoreType core, uint32 entry)
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : core - core to reset
 *                      entry - vector table address for CR52 core and remap address for lp core
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : errID
 *
 * Description        : This function kickcore.
 * \endverbatim
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_SocKickCore(Mcu_KickCoreType core, uint32 entry)
{
    Std_ReturnType errStatus;

    errStatus = Mcu_Ip_SetCoreVect(core, entry);
    ASM_KEYWORD("dsb sy");
    ASM_KEYWORD("isb sy");

    if (E_OK == errStatus)
    {
        if (KICK_CPU_INDEX_0 == core)
        {
            errStatus = Mcu_Ip_ResetCtlReset(&rstsig_cr52p0);
        }
        else if (KICK_CPU_INDEX_1 == core)
        {
            errStatus = Mcu_Ip_ResetCtlReset(&rstsig_cr52p1);
        }
        else if (KICK_CPU_INDEX_2 == core)
        {
            errStatus = Mcu_Ip_ResetCtlReset(&rstsig_cr52p2);
        }
        else if (KICK_CPU_INDEX_3 == core)
        {
            errStatus = Mcu_Ip_ResetCtlReset(&rstsig_cr52p3);
        }
        else if (KICK_CPU_INDEX_4 == core)
        {
            errStatus = Mcu_Ip_ResetCtlReset(&rstsig_cr5_se);
        }
        else /*if (KICK_CPU_INDEX_5 == core)*/
        {
            errStatus = Mcu_Ip_ResetCtlReset(&rstsig_cr5_lp);
        }
    }

    return errStatus;
}
#define MCU_STOP_SEC_CODE
#include "Mcu_MemMap.h"
/* End of file */
